<?php
declare (strict_types = 1);

namespace app\command;

use app\common\models\Community\CommunityUser;
use app\common\models\Member\Account;
use app\common\models\Member\Member;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class StatsPersonMemberTotal extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('stats:person:member:total')
            ->setDescription('the statspersonmembertotal command');
    }

    protected function execute(Input $input, Output $output)
    {
        $list = Member::getInstance()->where('is_partner',1)->select();
        foreach ($list as $v){
            $ids = $this->getDiGui($v['id']);
            $ids[] = $v['id'];
            sort($ids);
            try{
                foreach($ids as $id){
                    $real_name = Account::getInstance()->where('user_id',$id)->value('real_name');
                    if(empty($real_name)){
                        continue;
                    }
                    $person = Db::table('stats_person_member_data')->where('user_id',$id)->find();
                    $partner_money = CommunityUser::getInstance()->where('user_id',$id)->where('community_status',8)->sum('total_amount') ?: 0;
                    $is_partner = Member::getInstance()->where('id',$id)->value('is_partner') ? '是':'否';
                    $invite_id = Member::getInstance()->where('id',$id)->value('invite_id');
                    $invite_name = Account::getInstance()->where('user_id',$invite_id)->value('real_name');
                    $ret = Db::table('stats_person_member_total')->where('user_id',$id)->find();
                    $data = [
                        'phone' => Member::getInstance()->where('id',$id)->value('phone'),
                        'real_name' => $real_name,
                        'recharge_total' => ($person['recharge_total_sand'] ?? 0) + ($person['recharge_total_balance'] ?? 0) + ($person['alipay'] ?? 0),
                        'tx_total' => ($person['amount_to_alipay'] ?? 0) + ($person['sand_to_card'] ?? 0),
                        'partner_money' => $partner_money,
                        'is_partner' => $is_partner,
                        'invite_name' => $invite_name,
                        'team_phone' => $v['phone'],
                        'user_id' => $id,
                    ];
                    if($ret){
                        Db::table('stats_person_member_total')->where('user_id',$id)->update($data);
                    }else{
                        Db::table('stats_person_member_total')->insert($data);
                    }
                }
            }catch (\Exception $e){
                $output->writeln($e->getMessage().$e->getLine());
            }

        }
        // 指令输出
        $output->writeln('statspersonmembertotal');
    }

    public function getDiGui(int $id = 0, array &$result = array()): array
    {
        $res = Member::getInstance()->where('invite_id', $id)->where('is_partner',0)->field('id')->select();
        foreach ($res as $v) {
            if ($v) {
                $result[] = $v;
                $this->getDiGui($v['id'], $result);
            }
        }
        //不包括自己
        //$arr[] = $id;
        return array_column($result, 'id');
    }

}
